package com.jams.goodox.activity.mine.buy;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.OSS;
import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
import com.alibaba.sdk.android.oss.callback.OSSProgressCallback;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSPlainTextAKSKCredentialProvider;
import com.alibaba.sdk.android.oss.internal.OSSAsyncTask;
import com.alibaba.sdk.android.oss.model.AppendObjectRequest;
import com.alibaba.sdk.android.oss.model.AppendObjectResult;
import com.alibaba.sdk.android.oss.model.ObjectMetadata;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.SePref;
import com.jams.goodox.net.BaseEntity;
import com.jams.goodox.net.ProtocolManagers;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.MineService;
import com.jams.goodox.utils.StatusBarCompat;
import com.jams.goodox.utils.StringUtils;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.ToastUtil;

import butterknife.Bind;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * Created by Administrator on 2018/5/21.
 */

public class RecordNextStepActivity extends BaseActivity {
    String filePath;
    @Bind(R.id.edit_price)
    EditText editPrice;
    @Bind(R.id.edit_intro)
    EditText editIntro;
    @Bind(R.id.tx_submit)
    TextView txSubmit;

    String title;
    int time;

    @Override
    protected int getLayoutId() {
        return R.layout.record_step_activity;
    }

    @Override
    protected void init() {
        StyleUtil.titleBackKey(this, "课程录制");
        StatusBarCompat.compat(this, ContextCompat.getColor(this, R.color.main_color));//沉侵式头部
        filePath = getIntent().getStringExtra("filePath");
        title= getIntent().getStringExtra("title");
        time=getIntent().getIntExtra("time",0);
    }

    @OnClick(R.id.tx_submit)
    public void onClick(){
        String price=editPrice.getText().toString().trim();
        String intro=editIntro.getText().toString().trim();
        double priceF= StringUtils.toDouble(price);
        if(priceF==0) {
            ToastUtil.show("请输入正确的价格");
            return;
        }
        if(TextUtils.isEmpty(intro)){
            ToastUtil.show("请输入简介");
            return;
        }
        uploadMps(intro,priceF);
    }

    private String accessKeyId="LTAIqwDuB1xjCMLg";
    private String accessKeySecret="9uYZUtMznAa6pJOcmZBpbWN3O44P0N";
    A a;
    private void uploadMps(String intro,double priceF){
        final String objectKey=String.format("testfile/%s_%s.mp3", SePref.getId(),String.valueOf(System.currentTimeMillis()));
        AppendObjectRequest append = new AppendObjectRequest("hy-pic", objectKey, filePath);
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentType("audio/mp3");
        append.setMetadata(metadata);
// 设置追加位置
        append.setPosition(0);
        a=new A();
        a.setTitle(title);
        a.setTimeLength(String.valueOf(time));
        a.setMemberId(String.valueOf(SePref.getId()));
        a.setDescr(intro);
        a.setPrice(priceF);

        append.setProgressCallback(new OSSProgressCallback<AppendObjectRequest>() {
            @Override
            public void onProgress(AppendObjectRequest request, long currentSize, long totalSize) {
                Log.d("AppendObject", "currentSize: " + currentSize + " totalSize: " + totalSize);
            }
        });
        OSSCredentialProvider credentialProvider = new OSSPlainTextAKSKCredentialProvider(accessKeyId, accessKeySecret);
        OSS oss = new OSSClient(this, "oss-cn-shenzhen.aliyuncs.com", credentialProvider);
        showLoading();
        oss.asyncAppendObject(append, new OSSCompletedCallback<AppendObjectRequest, AppendObjectResult>() {
            @Override
            public void onSuccess(AppendObjectRequest request, AppendObjectResult result) {
                Log.d("AppendObject", "AppendSuccess");
                Log.d("NextPosition", "" + result.getNextPosition());
                a.radioLink=objectKey;
                realUpload(a);
            }
            @Override
            public void onFailure(AppendObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
                // 异常处理
                misLoading();
                ToastUtil.show("文件上传失败");
            }
        });
    }

    private void realUpload(A a){
        enqueue(createCallService(MineService.class).uploadRecord(ProtocolManagers.getRequestMap(a)), new RetrofitCallbackListener<BaseEntity>() {
            @Override
            public void onSuccess(Call<BaseEntity> call, BaseEntity result) {
                misLoading();
                setResult(RESULT_OK);
                finish();
            }

            @Override
            public void onError(String msg) {
                misLoading();
                ToastUtil.show("上传失败");
            }
        });
    }

    private static class A{
        String memberId;
        String  descr;
        String title;
        double price;
        String timeLength;
        String	String;
        String radioLink;

        public java.lang.String getMemberId() {
            return memberId;
        }

        public void setMemberId(java.lang.String memberId) {
            this.memberId = memberId;
        }

        public java.lang.String getDescr() {
            return descr;
        }

        public void setDescr(java.lang.String descr) {
            this.descr = descr;
        }

        public java.lang.String getTitle() {
            return title;
        }

        public void setTitle(java.lang.String title) {
            this.title = title;
        }

        public double getPrice() {
            return price;
        }

        public void setPrice(double price) {
            this.price = price;
        }

        public java.lang.String getTimeLength() {
            return timeLength;
        }

        public void setTimeLength(java.lang.String timeLength) {
            this.timeLength = timeLength;
        }

        public java.lang.String getString() {
            return String;
        }

        public void setString(java.lang.String string) {
            String = string;
        }
    }
}
